package cn.edu.sdjzu.xg.bysj.dao;

import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectCategory;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectTrueness;
import util.JdbcHelper;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

public class GraduateProjectTruenessDao {
    private static GraduateProjectTruenessDao graduateProjectTruenessDao = new GraduateProjectTruenessDao();
    private GraduateProjectTruenessDao(){}
    public static GraduateProjectTruenessDao getInstance(){
        return graduateProjectTruenessDao;
    }
    //数据库链接对象
    private Connection connection = null;
    //语句盒子
    private Statement statement = null;
    //预编译语句盒子
    private PreparedStatement preparedStatement = null;
    //存储过程语句盒子
    private CallableStatement callableStatement = null;
    //结果集
    private ResultSet resultSet = null;
    /**
     * 查找
     * @param connection
     * @param id
     * @return
     * @throws SQLException
     */
    public GraduateProjectTrueness find(Connection connection,int id) throws SQLException {
        GraduateProjectTrueness graduateProjectTrueness = null;
        //预编译语句
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM GraduateProjectTrueness WHERE id=?");
        preparedStatement.setInt(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            String description = resultSet.getString("description");
            String no = resultSet.getString("no");
            String remarks = resultSet.getString("remarks");
            graduateProjectTrueness = new GraduateProjectTrueness(id,description,no,remarks);
        }
        JdbcHelper.close(resultSet,preparedStatement);
        return graduateProjectTrueness;
    }
    //显示所有数据
    public Collection< GraduateProjectTrueness> findAll() throws SQLException {
        //graduateProjectTypes 集合用于储存查找到的数据
        Collection< GraduateProjectTrueness> graduateProjectTruenesses = new ArrayList<>();
        //获得数据库连接对象
        connection = JdbcHelper.getConn();
        //创建语句盒子
        statement = connection.createStatement();
        //执行sql语句并获得结果集
        resultSet = statement.executeQuery("select * from  GraduateProjectTruenesse");
        //读取数据
        while (resultSet.next()){
            //新建foundGraduateProjectType对象，用于储存查找到的每一行的信息
            GraduateProjectTrueness foundGraduateProjectTrueness =
                    new   GraduateProjectTrueness(resultSet.getInt("id"),
                            resultSet.getString("description"),
                            resultSet.getString("no"),
                            resultSet.getString("remarks"));
            //添加进集合中
            graduateProjectTruenesses.add(foundGraduateProjectTrueness);
        }
        //关闭结果集，语句盒子和数据库连接
        JdbcHelper.close(resultSet,statement,connection);
        //返回查找到的数据集合
        return graduateProjectTruenesses;
    }
}
